package com.senyint.hip.fhir.web;

import java.util.ArrayList;
import java.util.List;

import org.hl7.fhir.r4.model.*;
import org.hl7.fhir.exceptions.FHIRException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.senyint.hip.fhir.interceptor.CaptureInterceptor;
 
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.impl.GenericClient;

/**
 * 
 * *******description*******
 * 
 * *************************
 * 
 * @author liuchaogang
 * @date 2018年10月31日 下午3:02:41
 *
 */
@RestController
@RequestMapping("/procedureRequest")
public class ProcedureRequestController extends FhirBaseController {

	/*@PostMapping()
	public String createProcedureRequest(@RequestBody String param) throws FHIRException {
 
		ProcedureRequest p = convertProcedureRequest(param);

		CaptureInterceptor interceptor = new CaptureInterceptor();
		GenericClient client = newClient(tempData.getBaseServer(), interceptor);

		MethodOutcome out = client.create().resource(p).execute();
	
		return new Gson().toJson(out);
	}
	
	
	   @PutMapping()
	    public String updateProcedureRequest(@RequestBody String param) throws FHIRException {

		   ProcedureRequest p = convertProcedureRequest(param);

	        CaptureInterceptor interceptor = new CaptureInterceptor();
	        GenericClient client = newClient(tempData.getBaseServer(), interceptor);

	        MethodOutcome out = client.update(p.getId(), p);

	        return new Gson().toJson(out);
	    }

		protected ProcedureRequest convertProcedureRequest(String param) throws FHIRException {
			 JSONObject jsonObject = JSON.parseObject(param);
	    
	      
			ProcedureRequest procedureRequest = new ProcedureRequest();
		 	procedureRequest.setId(jsonObject.getString("id"));
			procedureRequest.setStatus(ProcedureRequestStatus.fromCode(jsonObject.getString("status")));
			procedureRequest.setIntent(ProcedureRequestIntent.fromCode(jsonObject.getString("intent")));
			// 检查项目
			CodeableConcept item = new CodeableConcept();
			item.addCoding(new Coding().setCode(jsonObject.getString("checkCode")).setDisplay(jsonObject.getString("checkDisplay"))
					.setSystem("http://loinc.org"));

			procedureRequest.setCode(item);
			// 患者信息
			procedureRequest.setSubject(
					new Reference().setDisplay(jsonObject.getString("patientName")));
			// 就诊信息
			procedureRequest.setContext(new Reference());
		 
		 
			 
			procedureRequest.setOccurrence(new DateTimeType(jsonObject.getDate("occurrenceDateTime")));
			// 请求时间
			procedureRequest.setAuthoredOn(jsonObject.getDate("requestDateTime"));
			// 申请医生

		 

			ProcedureRequestRequesterComponent pro = new ProcedureRequestRequesterComponent();
			pro.setAgent(new Reference().setDisplay(jsonObject.getString("requestDoctorName")));

			procedureRequest.setRequester(pro);
			
			//申请原因
			List<CodeableConcept> theReasonCode=new ArrayList<>();
			CodeableConcept codeableConcept=new CodeableConcept();
			 
		
			codeableConcept.setText(jsonObject.getString("reasonText"));
			theReasonCode.add(codeableConcept);
			procedureRequest.setReasonCode(theReasonCode);
			
			//检查部位
			List<CodeableConcept> theBodySite=new ArrayList<>();
			CodeableConcept codeableConceptBodySite=new CodeableConcept();
			List<Coding> theCoding=new ArrayList<>();
			theCoding.add(new Coding().setCode(jsonObject.getString("checkPartCode")).setDisplay(jsonObject.getString("checkPartDisplay")).setSystem("http://snomed.info/sct"));
			
	 
			
			codeableConceptBodySite.setCoding(theCoding);
			
			theBodySite.add(codeableConceptBodySite);
			procedureRequest.setBodySite(theBodySite);

			return procedureRequest;
		}*/
	 
}
